var APP = {};
APP.openDb = function(b) {
	var a = api.require("fs");
	a.exist({
		path: "fs://res/data.db"
	}, function(d, e) {
		if (d.exist) {
			var c = api.require("db");
			c.openDatabase({
				name: "data",
				path: "fs://res/data.db"
			}, function(f, g) {
				if (f.status) {
					b(c)
				} else {
					api.toast({
						msg: g.msg
					})
				}
			})
		} else {
			a.copyTo({
				oldPath: "widget://res/data.db",
				newPath: "fs://res/"
			}, function(f, g) {
				APP.openDb(b);
			});
			api.toast({
				msg: "数据初始化成功!",
				location: "middle"
			});
		}
	})
};
APP.openQDb = function(b) {
	var c = api.require("db");
	c.openDatabase({
		name: "data",
		path: "widget://res/data.db"
	}, function(f, g) {
		if (f.status) {
			b(c)
		} else {
			api.toast({
				msg: g.msg
			})
		}
	})
}

APP.loki = function(method, d, f) {
	db = new loki('data');
	db.loadDatabase({}, function() {
		c = db.getCollection('answers');
		if (c == null) {
			c = db.addCollection('answers');
		}
		switch (method) {
			case 'where':
				res = c.where(func);
				break;
			case 'find':
				res = c.find(d);
				break;
			case 'findOne':
				res = c.findOne(d);
				break;
			case 'get':
				res = c.get(d);
				break;
			default:
				break;
		}
	});
	return res;
}